package com.sigerap.ticket.service;

import java.util.List;



import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;

import com.sigerap.ticket.model.Ticket;
import com.sigerap.ticket.model.dao.TicketDAO;
import com.sigerap.ticket.model.dao.jpa.JPATicketDAO;
import com.sigerap.common.util.*;

/**
 * 
 * @author Spirit-20121
 *
 */

public class TicketService {
	private EntityManager em;
	private TicketDAO ticketDAO;

	public TicketService() {
		em = Utilitario.getInstance().getEntityManager();
		ticketDAO = new JPATicketDAO(em);
	}

	public List<Ticket> listarTickets() {
		// TODO Auto-generated method stub
		return ticketDAO.findAll();
	}

	public Ticket buscarTicketByNumero(Ticket p) {
		// TODO Auto-generated method stub
		return ticketDAO.findByNumero(p);
	} 

	public Ticket registrarTicket(Ticket p){
		Ticket newTicket=null;
		EntityTransaction tx= em.getTransaction();
		try{
			tx.begin();
			newTicket =ticketDAO.insert(p);
			tx.commit();
		}catch(Exception ex){
			tx.rollback();
			ex.printStackTrace();
			throw new RuntimeException("No se pudo eliminar");
		}
		return newTicket;
	}

}
